unit TPS_Edit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, ZAbstractTable, ZDataset, DB, ZAbstractRODataset,
  ZAbstractDataset, ZConnection, Setting, TPS_Pindah, TPS_Tambah_Orang, TPS_PindahOrang,
  cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxClasses, cxControls, cxGridCustomView, cxGridDBTableView, cxGrid;

type
  TPemilih = record
     KODE:String;
     ID:String;
     NAMA:String;
     NKK:String;
     RW:String;
     RT:String;
  end;
  TTPemilih = array of TPemilih;
  TPemilih2 = record
     ID:String;
     NIK:String;
     NKK:String;
     NAMA:String;
     TEMPAT:String;
     TANGGAL:String;
     IBU:String;
     AGAMA:String;
     KAWIN:String;
     KELAMIN:String;
     KELAINAN:String;
     ALAMAT:String;
     KOTA:String;
     KEC:String;
     DESA:String;
     RW:String;
     RT:String;
     KET:String;
  end;
  TTPemilih2 = array of TPemilih2;
  TPenyimpan = record
     ID:String;
     NAMA:String;
  end;
  TTPenyimpan = array of TPenyimpan;
  TDesa = record
     KODE:String;
     NAMA:String;
  end;
  TTDesa = array of TDesa;
  TeditTps = class(TForm)
    GroupBox1: TGroupBox;
    ZConnection1: TZConnection;
    ZQuery1: TZQuery;
    ZTable1: TZTable;
    ZQuery2: TZQuery;
    ZTable2: TZTable;
    btnHapus: TButton;
    btnTambah: TButton;
    btnPindah: TButton;
    btnBatal: TButton;
    btnUbah: TButton;
    GroupBox3: TGroupBox;
    rwcombo: TComboBox;
    rtcombo: TComboBox;
    Label4: TLabel;
    Label5: TLabel;
    Label3: TLabel;
    Label1: TLabel;
    StringGrid1: TStringGrid;
    Button2: TButton;
    Edit1: TEdit;
    Button1: TButton;
    Label2: TLabel;
    btnTambahSemua: TButton;
    btnTambahOrang: TButton;
    lblPemilih: TLabel;
    sgTPS1: TStringGrid;
    Btnprev: TButton;
    txtHal: TEdit;
    btnnext: TButton;
    lblhal: TLabel;
    btnRefresh: TButton;
    GroupBox2: TGroupBox;
    lblKet: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    txtAlamatTPS: TEdit;
    cbtps: TComboBox;
    ZQuery3: TZQuery;
    ZQuery4: TZQuery;
    lblTps: TLabel;
    cxGrid2: TcxGrid;
    cxGrid2DBTableView1: TcxGridDBTableView;
    cxGridTableView1: TcxGridTableView;
    cxGridColumn1: TcxGridColumn;
    cxGridColumn2: TcxGridColumn;
    cxGridColumn3: TcxGridColumn;
    cxGridColumn4: TcxGridColumn;
    cxGridColumn5: TcxGridColumn;
    cxGridColumn6: TcxGridColumn;
    cxGridColumn7: TcxGridColumn;
    cxGridColumn8: TcxGridColumn;
    cxGridColumn9: TcxGridColumn;
    cxGridColumn10: TcxGridColumn;
    cxGridColumn11: TcxGridColumn;
    cxGridColumn12: TcxGridColumn;
    cxGridColumn13: TcxGridColumn;
    cxGridColumn14: TcxGridColumn;
    cxGridColumn15: TcxGridColumn;
    cxGridColumn16: TcxGridColumn;
    cxGridColumn17: TcxGridColumn;
    cxGridColumn18: TcxGridColumn;
    cxGridColumn19: TcxGridColumn;
    cxGrid2Level1: TcxGridLevel;
    dsRT: TDataSource;
    dsTPS: TDataSource;
    qryRT: TZQuery;
    qryTPS: TZQuery;
    cxGrid1: TcxGrid;
    cxGrid1DBTableView1: TcxGridDBTableView;
    cxGridTableView2: TcxGridTableView;
    cxGridColumn20: TcxGridColumn;
    cxGridColumn21: TcxGridColumn;
    cxGridColumn22: TcxGridColumn;
    cxGridColumn23: TcxGridColumn;
    cxGridColumn24: TcxGridColumn;
    cxGridColumn25: TcxGridColumn;
    cxGridColumn26: TcxGridColumn;
    cxGridColumn27: TcxGridColumn;
    cxGridColumn28: TcxGridColumn;
    cxGridColumn29: TcxGridColumn;
    cxGridColumn30: TcxGridColumn;
    cxGridColumn31: TcxGridColumn;
    cxGridColumn32: TcxGridColumn;
    cxGridColumn33: TcxGridColumn;
    cxGridColumn34: TcxGridColumn;
    cxGridColumn35: TcxGridColumn;
    cxGridColumn36: TcxGridColumn;
    cxGridColumn37: TcxGridColumn;
    cxGridColumn38: TcxGridColumn;
    cxGrid1Level1: TcxGridLevel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnCloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BtnprevClick(Sender: TObject);
    procedure btnnextClick(Sender: TObject);
    procedure txtHalKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure btnHapusClick(Sender: TObject);
    procedure btnTambahClick(Sender: TObject);
    procedure btnPindahClick(Sender: TObject);
    procedure btnUbahClick(Sender: TObject);
    procedure btnBatalClick(Sender: TObject);
    procedure sgTPS1Click(Sender: TObject);
    procedure btnRefreshClick(Sender: TObject);
    procedure btnTambahOrangClick(Sender: TObject);
    procedure btnTambahSemuaClick(Sender: TObject);
    procedure rwcomboChange(Sender: TObject);
    procedure rtcomboChange(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure refreshdata;
    procedure cbtpsChange(Sender: TObject);
    procedure updateTanggal(iduser: String; desactive:string);
    function HandleSpec(Asal: String):String;
    procedure cxGrid1DBTableView1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    procedure TampilHalaman(Hal:Integer = 1);
    //procedure DeletePemilih1(var X: TTPemilih; const Index: Integer);
    //procedure DeletePemilih2(var X: TTPemilih; const Index: Integer);
    //procedure addPemilih(var dari: TTPemilih; ke: TTPemilih;const Index: Integer);
    procedure AutoSizeCol(Grid: TStringGrid; Column: integer);
    procedure getData;
    procedure getPage;
    procedure FetchDB;
    procedure TampilHalamanTambah(Hal: Integer = 1);
    procedure getPage2;
    { Private declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent; tid: String);             overload;
  end;

var
  editTps: TeditTps;
  sKabkot:string;
  skec:string;
  sdesa,srw,srt:string;
  listtpsid:tstringlist;
  tps1:string;
  tps:string;
  crow:integer;
  drow:integer;
  cHal:Integer; //current halaman
  jHal:Integer; //jumlah halaman
  bHal:Integer; //batas per halaman
  bTPS1:TTPemilih; //grid 1
  bTPS2:TTPemilih; //grid 2
  //-------------------------
  crow2:integer;
  drow2:integer;
  cHal2:Integer; //current halaman
  jHal2:Integer; //jumlah halaman
  bHal2:Integer; //batas per halaman
  bPemilih:TTPemilih2; //grid 1
  bTPS:TTPemilih2; //grid 2
  ct:Integer;
  ptable1,ptable2:String;
  pid1,pid2:String;
  fdata2:Boolean;
  selectedrow1,selectedrow2:integer;
  tambahorang:ttambah_orang;
  pindahorang:TTPS_Pindah;
  pindahorg:TForm_PindahOrang;

implementation

{$R *.dfm}

constructor TeditTps.Create(AOwner: TComponent; tid: String);
begin
    tps:=tid;
    inherited Create(AOwner);
end;

procedure TeditTps.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = VK_RETURN then
    getPage2;
end;

procedure TeditTps.getPage2();
begin
    if ((Edit1.Text='') or (StrToInt(Edit1.Text)>jHal) or (StrToInt(Edit1.Text)<1)) then
      ShowMessage('Isi dahulu dengan halaman yang sesuai')
    else if (jHal2=0) then
      ShowMessage('Tidak ada halaman yang dapat ditampilkan')
    else
      begin
        TampilHalamanTambah(StrToInt(Edit1.Text));
      end;
end;

procedure TeditTps.btnBatalClick(Sender: TObject);
begin
  Close;
end;

procedure TeditTps.btnCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TeditTps.updateTanggal(iduser: string; desactive:string);
var
  idtgl, tanggal : string;
begin
  //masukin tanggal
  tanggal := FormatDateTime('yyyy-mm-dd hh:mm:ss', Now());
  with ZQuery1 do  //masukkin nih gan
  begin
          Active:=False;
          sql.Clear;
          sql.Text := 'INSERT INTO tanggal (TANGGAL) VALUES ("'+tanggal+'")';
          ExecSQL;
  end;
  with ZQuery1 do  //masukkin nih gan
        begin
          Active:=False;
          sql.Clear;
          sql.Text := 'SELECT * FROM tanggal ORDER BY ID_TANGGAL DESC';
          Active:=True;
          idtgl := FieldByName('ID_TANGGAL').AsString;  //dapat ID_TGL
  end;
  with ZQuery1 do  //masukkin nih gan
  begin
          Active:=False;
          sql.Clear;
          sql.Text := 'UPDATE pemilih_'+desactive+' SET ID_TANGGAL = '+idtgl+' WHERE ID_USER='+QuotedStr(iduser);
          execsql;
  end;
end;

procedure TeditTps.btnHapusClick(Sender: TObject);
var
  pemilihs,kodes : TStringList;
  que,sql_desa:String;
  buttonSelected : Integer;
  idxawal, idxakhir, iter,idx : integer;
  iduser : string;
  tanggal, idtgl, desactive:string;
begin
  //masukin tanggal dulu gan
  tanggal := FormatDateTime('yyyy-mm-dd hh:mm:ss', Now());
  with ZQuery1 do  //masukkin nih gan
  begin
    Active:=False;
    sql.Clear;
    sql.Text := 'INSERT INTO tanggal (TANGGAL) VALUES ("'+tanggal+'")';
    ExecSQL;
  end;
  with ZQuery1 do  //masukkin nih gan
  begin
    Active:=False;
    sql.Clear;
    sql.Text := 'SELECT * FROM tanggal ORDER BY ID_TANGGAL DESC';
    Active:=True;
    idtgl := FieldByName('ID_TANGGAL').AsString;  //dapat ID_TGL
  end;

  if (cxGrid1DBTableView1.Controller.SelectedRowCount>0) and (sdesa<>'') then begin
    pemilihs := Tstringlist.Create;
    kodes := Tstringlist.Create;
    with cxGrid1DBTableView1.Controller do begin
        for idx:=0 to SelectedRowCount-1 do begin
          iduser:=SelectedRows[idx].Values[0];
          desactive := SelectedRows[idx].Values[13]; //ntar diganti
          //desactive:=SelectedRows[idx];
          //cari kode desa
          sql_desa := 'SELECT KODE_DESA FROM desa WHERE KODE_KECAMATAN = '+quotedStr(copy(sdesa,1,5))+' AND TAMPIL = '+quotedStr(desactive);
          //showmessage(sql_desa);
          zQuery1.Active:=false;
          zQuery1.SQL.Clear;
          zQuery1.SQL.Text:=sql_desa;
          zQuery1.Active:=true;
          zQuery1.First;
          desactive:=zQuery1.fieldbyname('KODE_DESA').AsString;
          kodes.Add(desactive);
          pemilihs.Add(iduser);
        end;
    end;
    buttonSelected := MessageDlg('Apakah anda yakin akan menghapus pemilih?',mtConfirmation , mbOKCancel, 0);
    if buttonSelected = mrOK  then begin
    //kalo banyak
    for iter := 0 to pemilihs.Count-1 do begin
      iduser := pemilihs[iter];
      desactive := kodes[iter];
      with ZQuery1 do
      begin
        Active:=False;
        sql.Clear;
        sql.Text:='UPDATE pemilih_'+desactive+' SET TPS=0, ID_TANGGAL = '+idtgl+' WHERE ID_USER='+QuotedStr(iduser);
        ExecSQL;
      end;
      updateTanggal(iduser, desactive);
      //showmessage(iduser);
    end;
    //fetch tampilan
    getData;
    if (ct>=0) then TampilHalaman(1);
    showmessage('berhasil menghapus '+inttostr(pemilihs.Count)+' pemilih dari TPS');
    end;
  end
  else
  begin
    showmessage('Pilih pemilih terlebih dahulu');
  end;
end;

procedure TeditTps.btnnextClick(Sender: TObject);
begin
  TampilHalaman(cHal+1);
  txtHal.Text:=IntToStr(cHal);
end;

procedure TeditTps.btnPindahClick(Sender: TObject);
var
  pemilihs, kodes : tstringlist;
  iduser, desactive, sql_desa : string;
  idx: integer;
begin
  if (cxGrid1DBTableView1.Controller.SelectedRowCount>0) and (sdesa<>'') then begin
    pemilihs := Tstringlist.Create;
    kodes := Tstringlist.Create;
    with cxGrid1DBTableView1.Controller do begin
        for idx:=0 to SelectedRowCount-1 do begin
          iduser:=SelectedRows[idx].Values[0];
          desactive := SelectedRows[idx].Values[13];
          //desactive:=SelectedRows[idx];
          //cari kode desa
          sql_desa := 'SELECT KODE_DESA FROM desa WHERE KODE_KECAMATAN = '+quotedStr(copy(sdesa,1,5))+' AND TAMPIL = '+quotedStr(desactive);
          //showmessage(sql_desa);
          zQuery1.Active:=false;
          zQuery1.SQL.Clear;
          zQuery1.SQL.Text:=sql_desa;
          zQuery1.Active:=true;
          zQuery1.First;
          desactive:=zQuery1.fieldbyname('KODE_DESA').AsString;
          kodes.Add(desactive);
          pemilihs.Add(iduser);
        end;
    end;
    //nah ini kita buka form nya
    pindahorg:=TForm_PindahOrang.Create(application,sdesa,'',tps,'',pemilihs,kodes);
  end
  else
  begin
    showmessage('Pilih pemilih terlebih dahulu');
  end;
  
  //if (sgTPS1.Row>0) then
//  pindahorang:=TTPS_Pindah.Create(application, sdesa, tps);
    //pindahorg:=TForm_PindahOrang.Create(application,sdesa,bTPS1[sgTPS1.Row].ID,tps,bTPS1[sgTPS1.Row].KODE);
end;

procedure TeditTps.BtnprevClick(Sender: TObject);
begin
  TampilHalaman(cHal-1);
  txtHal.Text:=IntToStr(cHal);
end;

procedure TeditTps.btnRefreshClick(Sender: TObject);
begin
  getdata;
  if (ct>=0) then tampilhalaman(1);
end;

procedure TeditTps.btnTambahClick(Sender: TObject);
begin
  tambahorang:=ttambah_orang.Create(application, sdesa, tps);
end;

function TeditTps.HandleSpec(Asal: String):String;
var
  temp:string;
begin
  temp := StringReplace(asal, '''', '',[rfReplaceAll, rfIgnoreCase]);
  temp := StringReplace(temp, '\', '',[rfReplaceAll, rfIgnoreCase]);
  result := temp;
end;

procedure TeditTps.btnUbahClick(Sender: TObject);
var
  que:String;
begin
  with zquery1 do begin
    active:=false;
    sql.Clear;
    que:='UPDATE daftar_tps SET ALAMAT='+QuotedStr(HandleSpec(txtAlamatTPS.Text))+
    ' WHERE ID_TPS='+tps;
    sql.Text:=que;
//    ShowMessage(que);
    ExecSQL;
  end;
  ShowMessage('Data TPS sudah disimpan');
  //perlu ada repaint form sebelumnya?
  Close;
end;

procedure TeditTps.btnTambahOrangClick(Sender: TObject);
var
  lolos:boolean;
  pemilihs:TStringList;
  vrow,i:integer;
  temp:TPemilih2;
  jumlahtps, inputan : integer;
  buttonSelected : integer;
  idxawal, idxakhir, iter, idx : integer;
  iduser : string;
  tanggal,idtgl:string;
begin
  if (cxGrid2DBTableView1.Controller.SelectedRowCount>0) and (sdesa<>'') then begin
    pemilihs := Tstringlist.Create;
    with cxGrid2DBTableView1.Controller do begin
        for idx:=0 to SelectedRowCount-1 do begin
          iduser:=SelectedRows[idx].Values[0];
          pemilihs.Add(iduser);
        end;
    end;
    buttonSelected := MessageDlg('Apakah Anda yakin akan menambahkan pemilih ke TPS?',mtConfirmation , mbOKCancel, 0);
    if(buttonSelected = mrOK) then begin
      //cek lebih atau ndak
      with ztable1 do
      begin
        active:=false;
        tablename:='pemilih_'+sdesa;
        filter:='TPS='+tps+' AND STATUS2=''0''';
        filtered:=true;
        active:=true;
        first;
        jumlahtps := RecordCount;
      end;
      inputan := pemilihs.Count;
      lolos:=true;
      if(copy(cbtps.Items[cbtps.ItemIndex],1,1) <> 'K') then begin
        if(jumlahtps+inputan>600) then
          lolos:=false;
      end;

      if(not lolos)then
      begin
        showmessage('Maaf data tidak dapat dimasukkan karena jumlah orang tps saat ini '+IntToStr(jumlahtps)+' sedangkan akan diinput sebanyak '+IntToStr(inputan)+' sehingga akan melebihi kuota tps');
      end
      else begin
        //masukin tanggal dulu gan
        tanggal := FormatDateTime('yyyy-mm-dd hh:mm:ss', Now());
        with ZQuery1 do  //masukkin nih gan
        begin
          Active:=False;
          sql.Clear;
          sql.Text := 'INSERT INTO tanggal (TANGGAL) VALUES ("'+tanggal+'")';
          ExecSQL;
        end;
        with ZQuery1 do  //masukkin nih gan
        begin
          Active:=False;
          sql.Clear;
          sql.Text := 'SELECT * FROM tanggal ORDER BY ID_TANGGAL DESC';
          Active:=True;
          idtgl := FieldByName('ID_TANGGAL').AsString;  //dapat ID_TGL
        end;
          for iter := 0 to pemilihs.Count-1 do begin
            iduser := pemilihs[iter];
            with ZQuery1 do
            begin
              Active:=False;
              sql.Clear;
              sql.Text:='UPDATE pemilih_'+sdesa+' SET TPS='+tps+' WHERE ID_USER='+QuotedStr(iduser);
              ExecSQL;

            end;
            //showmessage(iduser);
            updateTanggal(iduser,sdesa);
          end;

        fetchdb;
      //something happen here
        getdata;
        ShowMessage('Pemilih berhasil ditambahkan');
        if (ct>=0) then tampilhalaman(1);
      end;
    end;
  end
    else
  begin
    showmessage('Pilih pemilih terlebih dahulu');
  end;
  //idxawal := stringgrid1.selection.Top;
  //idxakhir := stringgrid1.selection.bottom;
  //showmessage(inttostr(idxawal) + inttostr(idxakhir));
  //buttonSelected := MessageDlg('Apakah Anda yakin akan menambahkan pemilih ke TPS?',mtConfirmation , mbOKCancel, 0);
  //if(buttonSelected = mrOK) then begin


  {if((selectedrow1<>0) and(StringGrid1.RowCount>=2) and(StringGrid1.Cells[0,1]<>''))then
  begin
    //cek bung lebih ndak
    temp:=bPemilih[selectedrow1];
    with ztable1 do
    begin
      active:=false;
      tablename:='pemilih_'+sdesa;
      filter:='ID_TPS='+tps;
      filtered:=true;
      active:=true;
      first;
      jumlahtps := RecordCount;
    end;
    if(temp.NKK<>'')then
    begin
      with ztable1 do
      begin
        active:=false;
        tablename:='pemilih_'+sdesa;
        filter:='NKK='+QuotedStr( temp.NKK);
        filtered:=true;
        active:=true;
        first;
        inputan := RecordCount;
      end;
    end
    else inputan := 1;

    if(jumlahtps+inputan>600)then
    begin
      showmessage('Maaf data tidak dapat dimasukkan karena jumlah orang tps saat ini '+IntToStr(jumlahtps)+' sedangkan akan diinput sebanyak '+IntToStr(inputan)+' sehingga akan melebihi kuota tps');
    end
    else
    begin
      if(temp.NKK<>'')then
      begin
        with ZQuery1 do
        begin
          Active:=False;
          sql.Clear;
          sql.Text:='UPDATE pemilih_'+sdesa+' SET TPS='+tps+' WHERE NKK='+QuotedStr(temp.NKK);
          ExecSQL;
        end;
      end
      else
      begin
        with ZQuery1 do
        begin
          Active:=False;
          sql.Clear;
          sql.Text:='UPDATE pemilih_'+sdesa+' SET TPS='+tps+' WHERE ID_USER='+QuotedStr(temp.ID);
          ExecSQL;
        end;
      end;
    end;

    fetchdb;
    //something happen here
    getdata;
    ShowMessage('Pemilih berhasil ditambahkan');
    if (ct>=0) then tampilhalaman(1);
  end;
  end;}
end;

procedure TeditTps.Button1Click(Sender: TObject);
begin
  TampilHalamanTambah(cHal2+1);
  Edit1.Text:=IntToStr(cHal2);
end;

procedure TeditTps.Button2Click(Sender: TObject);
begin
  TampilHalamanTambah(cHal2-1);
  Edit1.Text:=IntToStr(cHal2);
end;

procedure TeditTps.btnTambahSemuaClick(Sender: TObject);
var
  lolos:boolean;
  I:Integer;
  vrow:Integer;
  temp:TPemilih2;
  jumlahtps, inputan:integer;
  buttonSelected : integer;
  tanggal, idtgl : string;
begin
  //tambah RT/RW
  //cek RT/RW ini sudah ada di TPS lain gak???
  buttonSelected := MessageDlg('Apakah Anda yakin akan menambahkan RT?',mtConfirmation , mbOKCancel, 0);
  if(buttonSelected = mrOK) then begin
  if((sdesa<>'') and(srw<>'') and(srt<>'') )then
  begin
    //cek bung lebih ndak
    with ztable1 do
    begin
      active:=false;
      tablename:='pemilih_'+sdesa;
      filter:='TPS='+tps+' AND STATUS2=''0''';
      filtered:=true;
      active:=true;
      first;
      jumlahtps := RecordCount;
    end;
    with ztable1 do
    begin
      active:=false;
      tablename:='pemilih_'+sdesa;
      filter:='RW='+QuotedStr( srw)+' and RT='+QuotedStr(srt)+' AND TPS = 0 AND STATUS2 = 0';
      filtered:=true;
      active:=true;
      first;
      inputan := RecordCount;
    end;
    lolos:=true;
    if(copy(cbtps.Items[cbtps.ItemIndex],1,1) <> 'K') then begin
      if(jumlahtps+inputan>600) then
        lolos:=false;
    end;

    if(not lolos)then
    begin
      showmessage('Maaf data tidak dapat dimasukkan karena jumlah orang di TPS saat ini '+IntToStr(jumlahtps)+' sedangkan akan diinput sebanyak '+IntToStr(inputan)+' sehingga akan melebihi kuota TPS');
    end
    else
    begin
        with ZQuery1 do
        begin
          Active:=False;
          sql.Clear;
          sql.Text:='UPDATE pemilih_'+sdesa+' SET TPS='+tps+' WHERE RW='+QuotedStr(srw)+' and RT='+QuotedStr(srt)+' AND TPS = 0 AND STATUS2=0';
          ExecSQL;
        end;
        //masukin tanggal
  tanggal := FormatDateTime('yyyy-mm-dd hh:mm:ss', Now());
      with ZQuery1 do  //masukkin nih gan
        begin
          Active:=False;
          sql.Clear;
          sql.Text := 'INSERT INTO tanggal (TANGGAL) VALUES ("'+tanggal+'")';
          ExecSQL;
        end;
      with ZQuery1 do  //masukkin nih gan
        begin
          Active:=False;
          sql.Clear;
          sql.Text := 'SELECT * FROM tanggal ORDER BY ID_TANGGAL DESC';
          Active:=True;
          idtgl := FieldByName('ID_TANGGAL').AsString;  //dapat ID_TGL
        end;
      with ZQuery1 do  //masukkin nih gan
        begin
          Active:=False;
          sql.Clear;
          sql.Text := 'UPDATE pemilih_'+sdesa+' SET ID_TANGGAL = '+idtgl+' WHERE RW='+QuotedStr(srw)+' and RT='+QuotedStr(srt);
          execsql;
        end;
        ShowMessage('Pemilih Berhasil ditambahkan');
    end;

    fetchdb;
    //something happen here
    getdata;
    if (ct>=0) then tampilhalaman(1);

  end
    else
    ShowMessage('Pilih lokasi terlebih dahulu dan memiliki pemilih');
  end;
end;

{procedure TeditTps.DeletePemilih1(var X: TTPemilih; const Index: Integer) ;
begin
   if Index > High(X) then Exit;
   if Index < Low(X) then Exit;
   if Index = High(X) then
   begin
     SetLength(X, Length(X) - 1) ;
     Exit;
   end;
   Finalize(X[Index]) ;
   System.Move(X[Index+1], X[Index],(Length(X) - Index -1) * SizeOf(TPemilih) + 1) ;
   SetLength(X, Length(X) - 1);
   drow:=drow-1;
   sgTPS1.RowCount:=sgTPS1.RowCount-1;
end;

procedure TeditTps.addPemilih(var dari: TTPemilih; ke: TTPemilih; const Index: Integer);
var
  n:Integer;
begin
  n:=Length(ke);
  SetLength(ke,n+1);
  ke[n]:=dari[Index];
  if (dari=bTPS1) then
    drow:=drow+1
  else
    drow2:=drow2+1;
end;

procedure TeditTps.DeletePemilih2(var X: TTPemilih; const Index: Integer) ;
begin
   if Index > High(X) then Exit;
   if Index < Low(X) then Exit;
   if Index = High(X) then
   begin
     SetLength(X, Length(X) - 1) ;
     Exit;
   end;
   Finalize(X[Index]) ;
   System.Move(X[Index+1], X[Index],(Length(X) - Index -1) * SizeOf(TPemilih) + 1) ;
   SetLength(X, Length(X) - 1);
   drow2:=drow2-1;
   sgTPS2.RowCount:=sgTPS2.RowCount-1;
end; }

procedure TeditTps.txtHalKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if key = VK_RETURN then
    getPage;
end;

procedure TeditTps.getPage();
begin
    if ((txtHal.Text='') or (StrToInt(txtHal.Text)>jHal) or (StrToInt(txtHal.Text)<1)) then
      ShowMessage('Isi dahulu dengan halaman yang sesuai')
    else if (jHal=0) then
      ShowMessage('Tidak ada halaman yang dapat ditampilkan')
    else
      begin
        TampilHalaman(StrToInt(txtHal.Text));
      end;
end;

procedure TeditTps.rtcomboChange(Sender: TObject);
begin
  if (rtcombo.ItemIndex > 0) then
  begin
    srt := rtcombo.Items[rtcombo.ItemIndex];
    {ZQuery2.Active := False;
    ZQuery2.SQL.Text:='SELECT * FROM pemilih_'+sdesa+' where RW='+QuotedStr(srw)+' and RT='+QuotedStr(srt;
    ZQuery2.Active := True;
    ZQuery2.First;  //tabel simpan}
    fetchdb;
  end
    else
  begin
    cxGrid2DBTableView1.BeginUpdate;
    cxGrid2DBTableView1.DataController.Filter.Clear;
    cxGrid2DBTableView1.ClearItems;
    cxGrid2DBTableView1.EndUpdate;
    srt:='';
  end;
end;

procedure TeditTps.FetchDB();
var
  kode:String;
  query:String;
  kKota:String;
  kKec:String;
  kDesa:String;
  tblName:String;
  i:Integer;
  vRow2:Integer;
begin
  dRow2:=1;
  vRow2:=1;
  fData2:=False;
  jHal2:=0;
  StringGrid1.RowCount:=2;
  Label2.Caption:='-';
  //reset
  for i := 1 to StringGrid1.RowCount - 1
  do StringGrid1.Rows[i].Clear;
  //ganti
  if (sDesa<>'') then
    begin
      kode:=sDesa;
      kKota:='';//kabkotCombo.Items[kabkotCombo.ItemIndex];
      kKec:='';//kecCombo.Items[kecCombo.ItemIndex];
      kDesa:='';//keldesCombo.Items[keldesCombo.ItemIndex];
    end;
  query:='NAMA LIKE "pemilih_'+kode+'%"';
  ZQuery1.Active := False;
  query:= 'SELECT * FROM penyimpan WHERE '+query;
  ZQuery1.SQL.Text := query;
  ZQuery1.Active := True;
  ZQuery1.First;  //tabel simpan
  //ganti
    tblName:=ZQuery1.FieldByName('NAMA').AsString;
    qryRT.Active := False;
    qryRT.SQL.Text := 'SELECT ID_USER, NIK, NKK, NAMA, TEMPAT AS `TEMPAT_LAHIR`, TANGGAL AS `TANGGAL_LAHIR`, IBU, KAWIN AS `STATUS_PERKAWINAN`, KELAMIN AS `JENIS_KELAMIN`, ALAMAT, RW, RT, DUSUN FROM '+tblName+' WHERE (RW='+QuotedStr(srw)+' AND RT='+QuotedStr( srt )+' AND TPS=0 AND STATUS2 = ''0'') ORDER BY NKK';
    qryRT.Active := True;
//    ZQuery3.First;  //tabel simpan
    dsRT.DataSet := qryRT;

    label1.Caption:=inttostr(qryRT.RecordCount);

    //load si cxgridnya
    cxGrid2DBTableView1.BeginUpdate;
    cxGrid2DBTableView1.DataController.Filter.Clear;
    cxGrid2DBTableView1.ClearItems;

    cxGrid2DBTableView1.DataController.DataSource := dsRT;
    cxGrid2DBTableView1.DataController.CreateAllItems;
    cxGrid2DBTableView1.Columns[0].Width:=40;
    cxGrid2DBTableView1.Columns[1].Width:=105;
    cxGrid2DBTableView1.Columns[2].Width:=105;
    cxGrid2DBTableView1.Columns[3].Width:=120;
    cxGrid2DBTableView1.Columns[4].Width:=90;
    cxGrid2DBTableView1.Columns[6].Width:=120;
    cxGrid2DBTableView1.Columns[9].Width:=150;
    cxGrid2DBTableView1.Columns[12].Width:=150;
    cxGrid2DBTableView1.EndUpdate;

    {ZTable1.Active := False;

    ZTable1.TableName := tblName;
    ZTable1.Filter:='RW='+QuotedStr(srw)+' AND RT='+QuotedStr( srt )+' AND TPS=0';  //get query
    ZTable1.Filtered := TRUE;
    ZTable1.Active := True;
    ZTable1.First;  //tabel
    dRow2:=dRow2+Zquery3.RecordCount;
    SetLength(bPemilih,dRow2);
    Label1.Caption:=IntToStr(drow2-1);
    fData2:=True;
    while (not Zquery3.Eof) do
      begin
        bPemilih[vRow2].ID :=Zquery3.Fields[0].AsString;
        bPemilih[vRow2].NIK:=Zquery3.Fields[1].AsString;
        bPemilih[vRow2].NKK:=Zquery3.Fields[2].AsString;
        bPemilih[vRow2].NAMA:=Zquery3.Fields[3].AsString;
        bPemilih[vRow2].TEMPAT:=Zquery3.Fields[4].AsString;
        bPemilih[vRow2].TANGGAL:=Zquery3.Fields[5].AsString;
        bPemilih[vRow2].IBU:=Zquery3.Fields[6].AsString;
        bPemilih[vRow2].AGAMA:=Zquery3.Fields[7].AsString;
        bPemilih[vRow2].KAWIN:=Zquery3.Fields[8].AsString;
        bPemilih[vRow2].KELAMIN:=Zquery3.Fields[9].AsString;
        bPemilih[vRow2].KELAINAN:=Zquery3.Fields[10].AsString;
        bPemilih[vRow2].ALAMAT:=Zquery3.Fields[11].AsString;
        bPemilih[vRow2].KOTA:=kKota;
        bPemilih[vRow2].KEC:=kKec;
        bPemilih[vRow2].DESA:=kDesa;
        bPemilih[vRow2].RW:=Zquery3.Fields[12].AsString;
        bPemilih[vRow2].RT:=Zquery3.Fields[13].AsString;
        bPemilih[vRow2].KET:=Zquery3.Fields[14].AsString;
        Zquery3.Next;
        vRow2:=vRow2+1;
      end;
    if (fData2=True) then TampilHalamanTambah();}
end;

procedure TeditTps.TampilHalamanTambah(Hal:Integer = 1);
var
  iter,i:Integer;
begin
  Edit1.Enabled:=True;
  bHal2:=50;  //pemilih per halaman
  jHal2:=(dRow2-1) div bHal2;
  if (((dRow2-1) mod bHal2)>0) then jHal2:=jHal2+1;
  label2.Caption := IntToStr(Hal)+' of '+IntToStr(jHal2);
  edit1.Text := IntToStr(Hal);
  cHal2:=Hal;
  iter:=((Hal-1)*bHal2)+1;
  crow2:=1;
  if (cHal2=1) then
    Button2.Enabled:=False
  else
    button2.Enabled:=True;
  if (cHal2=jHal2) then
    button1.Enabled:=False
  else
    button1.Enabled:=True;
  while ((iter<dRow2) and (crow2<=bHal2)) do
   begin
    StringGrid1.RowCount:=crow2+1;
    StringGrid1.Cells[0,crow2]:=IntToStr(iter);
    StringGrid1.Cells[1,crow2]:=bPemilih[iter].NIK;
    StringGrid1.Cells[2,crow2]:=bPemilih[iter].NKK;
    StringGrid1.Cells[3,crow2]:=bPemilih[iter].NAMA;
    StringGrid1.Cells[4,crow2]:=bPemilih[iter].TEMPAT;
    StringGrid1.Cells[5,crow2]:=bPemilih[iter].TANGGAL;
    StringGrid1.Cells[6,crow2]:=bPemilih[iter].IBU;
    StringGrid1.Cells[7,crow2]:=bPemilih[iter].AGAMA;
    StringGrid1.Cells[8,crow2]:=bPemilih[iter].KAWIN;
    StringGrid1.Cells[9,crow2]:=bPemilih[iter].KELAMIN;
    StringGrid1.Cells[10,crow2]:=bPemilih[iter].KELAINAN;
    StringGrid1.Cells[11,crow2]:=bPemilih[iter].ALAMAT;
    StringGrid1.Cells[12,crow2]:=bPemilih[iter].KOTA;
    StringGrid1.Cells[13,crow2]:=bPemilih[iter].KEC;
    StringGrid1.Cells[14,crow2]:=bPemilih[iter].DESA;
    StringGrid1.Cells[15,crow2]:=bPemilih[iter].RW;
    StringGrid1.Cells[16,crow2]:=bPemilih[iter].RT;
    StringGrid1.Cells[17,crow2]:=bPemilih[iter].KET;
    StringGrid1.Cells[18,crow2]:=bPemilih[iter].ID;
    Inc(crow2);
    Inc(iter);
   end;
   for i := 0 to StringGrid1.ColCount - 1 do
    AutoSizeCol(StringGrid1, i);
end;

procedure TeditTps.rwcomboChange(Sender: TObject);
var
  i:Integer;
begin
if (rwcombo.ItemIndex > -1) then
  begin
    srw := rwcombo.Items[rwcombo.ItemIndex];

    rtcombo.Clear;
    rtcombo.Items.Add('PILIH RT');
    rtcombo.ItemIndex:=0;

    ZQuery2.Active := False;
    ZQuery2.SQL.Text:='SELECT DISTINCT(RT) FROM pemilih_'+sdesa+' where RW="'+srw+'" ORDER BY RT ASC';
    ZQuery2.Active := True;
    ZQuery2.First;  //tabel simpan
    while (not ZQuery2.Eof) do
    begin
      rtcombo.Items.Add(ZQuery2.FieldByName('RT').AsString);
      ZQuery2.Next;
    end;
    for i := 1 to StringGrid1.RowCount - 1
    do StringGrid1.Rows[i].Clear;
  end
    else
  begin
    srw:='';
    srt:='';
  end;
end;

procedure TeditTps.sgTPS1Click(Sender: TObject);
begin
  if (sgTPS1.Row>0) then btnHapus.Enabled:=true;
end;

procedure TeditTps.StringGrid1Click(Sender: TObject);
begin
  if ((StringGrid1.Row>0)and(stringgrid1.Cells[0,1]<>'')and(cHal>0)) then begin
      selectedrow1:=stringgrid1.Row+ ((cHal-1)*bHal);
  end;
end;

procedure TeditTps.TampilHalaman(Hal:Integer = 1);
var
  iter:Integer;
  query,queryw:String;
  kKota:String;
  kKec:String;
  kDesa:String;
  tblname,kode,tpemilih,ttps,tampildesa:String;
  iduser,kodpem:String;
  i,awal,total,ctr:Integer;
begin
  ctr:=0;
  total := 1;
  cRow:=2;
  cHal:=Hal;
  sgTPS1.RowCount:=crow;
  lblHal.Caption:='-';
  //reset
  for i := 1 to sgTPS1.RowCount - 1
  do sgTPS1.Rows[i].Clear;
  //
  query:='';
  qryTPS.Active := False;
  //awal:=(Hal-1)*bhal;
  ZQuery3.active:=false;
  Zquery3.SQL.Clear;
  Zquery3.SQL.Text:='SELECT * FROM desa WHERE KODE_KECAMATAN LIKE '+quotedstr(Copy(sdesa,1,5));
  Zquery3.Active:=true;
  zquery3.first;
  while (not zquery3.Eof) do begin
    queryw:='TPS ='+tps+' AND STATUS2=0';
    //tpemilih:='pemilih_'+sdesa;
    tpemilih:='pemilih_'+ZQuery3.fieldbyName('KODE_DESA').AsString;
    tampildesa:=ZQuery3.fieldbyName('TAMPIL').AsString;
    if(ctr > 0) then
      query:=query+' UNION ALL ';
    query:= query+'(SELECT ID_USER, NIK, NKK, NAMA, TEMPAT AS `TEMPAT_LAHIR`, TANGGAL AS `TANGGAL_LAHIR`, IBU, KAWIN AS `STATUS_PERKAWINAN`, KELAMIN AS `JENIS_KELAMIN`, ALAMAT, RW, RT, DUSUN, '+quotedStr(tampildesa)+' AS `KEL_DESA` FROM '+tpemilih+' WHERE '+queryw+' ORDER BY STATUS1, RW, RT, NKK, NIK ASC)';
    ctr:=ctr+1;
    zquery3.next;
  end;
  //showmessage(query);
  qryTPS.sql.text:=(query);
  qryTPS.Active := True;
  dsTPS.DataSet := qryTPS;

  cxGrid1DBTableView1.BeginUpdate;
  cxGrid1DBTableView1.DataController.Filter.Clear;
  cxGrid1DBTableView1.ClearItems;

  cxGrid1DBTableView1.DataController.DataSource := dsTPS;
  cxGrid1DBTableView1.DataController.CreateAllItems;
  cxGrid1DBTableView1.Columns[0].Width:=40;
   cxGrid1DBTableView1.Columns[1].Width:=105;
   cxGrid1DBTableView1.Columns[2].Width:=105;
   cxGrid1DBTableView1.Columns[3].Width:=120;
   cxGrid1DBTableView1.Columns[4].Width:=90;
   cxGrid1DBTableView1.Columns[6].Width:=120;
   cxGrid1DBTableView1.Columns[9].Width:=150;
   cxGrid1DBTableView1.Columns[12].Width:=150;
  cxGrid1DBTableView1.EndUpdate;
    //tabel simpan
  {total:=total+ZQuery2.RecordCount;
  SetLength(bTPS1,total);
    while (not ZQuery2.Eof) do begin
      sgTPS1.RowCount:=cRow;
        bTPS1[crow-1].KODE :=ZQuery3.FieldByName('KODE_DESA').AsString;
        bTPS1[crow-1].ID :=ZQuery2.FieldByName('ID_USER').AsString;
        bTPS1[crow-1].NAMA:=ZQuery2.FieldByName('NAMA').AsString;
        bTPS1[crow-1].NKK:=ZQuery2.FieldByName('NKK').AsString;
        bTPS1[crow-1].RW:=ZQuery2.FieldByName('RW').AsString;
        bTPS1[crow-1].RT:=ZQuery2.FieldByName('RT').AsString;
        //
        sgTPS1.Cells[0,crow-1]:=IntToStr((chal-1)*bhal+cRow-1);
        sgTPS1.Cells[1,crow-1]:=ZQuery2.FieldByName('NIK').AsString;
        sgTPS1.Cells[2,crow-1]:=ZQuery2.FieldByName('NKK').AsString;
        sgTPS1.Cells[3,crow-1]:=ZQuery2.FieldByName('NAMA').AsString;
        sgTPS1.Cells[4,crow-1]:=ZQuery2.FieldByName('TEMPAT').AsString;
        sgTPS1.Cells[5,crow-1]:=ZQuery2.FieldByName('TANGGAl').AsString;
        sgTPS1.Cells[6,crow-1]:=ZQuery2.FieldByName('IBU').AsString;
        sgTPS1.Cells[7,crow-1]:=ZQuery2.FieldByName('AGAMA').AsString;
        sgTPS1.Cells[8,crow-1]:=ZQuery2.FieldByName('KAWIN').AsString;
        sgTPS1.Cells[9,crow-1]:=ZQuery2.FieldByName('KELAMIN').AsString;
        sgTPS1.Cells[10,crow-1]:=ZQuery2.FieldByName('KELAINAN').AsString;
        sgTPS1.Cells[11,crow-1]:=ZQuery2.FieldByName('ALAMAT').AsString;
        sgTPS1.Cells[12,crow-1]:=ZQuery2.FieldByName('RW').AsString;
        sgTPS1.Cells[13,crow-1]:=ZQuery2.FieldByName('RT').AsString;
        sgTPS1.Cells[14,crow-1]:=ZQuery2.FieldByName('DUSUN').AsString;
        sgTPS1.Cells[15,crow-1]:=ZQuery2.FieldByName('KET').AsString;
        sgTPS1.Cells[16,crow-1]:=ZQuery3.FieldByName('TAMPIL').AsString;
        sgTPS1.Cells[17,crow-1]:=ZQuery2.FieldByName('ID_USER').AsString;
      inc(crow);
      ZQuery2.Next;
    end;
  zquery3.next;
  end;
  lblHal.Caption := IntToStr(Hal)+' of '+IntToStr(jHal);
  txtHal.Text := IntToStr(Hal);
  //-----------------------------------------
  txtHal.Enabled:=True;
  if (cHal=1) then
    btnPrev.Enabled:=False
  else
    btnPrev.Enabled:=True;
  if (cHal=jHal) then
    btnNext.Enabled:=False
  else
    btnNext.Enabled:=True;
  for i := 0 to sgTPS1.ColCount - 1 do
    AutoSizeCol(sgTPS1, i);}
end;

procedure TeditTps.AutoSizeCol(Grid: TStringGrid; Column: integer);
var
  i, W, WMax: integer;
begin
  WMax := 0;
  for i := 0 to (Grid.RowCount - 1) do begin
    W := Grid.Canvas.TextWidth(Grid.Cells[Column, i]);
    if W > WMax then
      WMax := W;
  end;
  Grid.ColWidths[Column] := WMax + 5;
end;

procedure TeditTps.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;


procedure TeditTps.FormCreate(Sender: TObject);
var
  setting:TSetting_Form;
  counter:integer;
begin
  setting:=TSetting_Form.Create;
  ZConnection1.HostName:=setting.host;
  ZConnection1.Protocol:=setting.protocol;
  ZConnection1.Database:=setting.database;
  ZConnection1.User:=setting.user;
  ZConnection1.Password:=setting.password;
  ZConnection1.LoginPrompt:=False;
  ZConnection1.Connected := True;
  ZQuery1.Connection:=ZConnection1;
  ZTable1.Connection:=ZConnection1;
  ZQuery2.Connection:=ZConnection1;
  ZTable2.Connection:=ZConnection1;
  qryRT.Connection:=ZConnection1;
  qryTPS.Connection:=ZConnection1;

  groupbox2.Color:= cl3DLight;

  refreshdata;
  counter:=0;
  listtpsid:=tstringlist.Create;
  with zquery1 do
  begin
    active:=false;
    sql.Clear;
    sql.text:='SELECT * FROM daftar_tps WHERE KODE_DESA='+QuotedStr(sdesa)+' ORDER BY NAMA+0';
    active:=true;
    first;

    while (not eof) do
    begin
      cbtps.Items.Add(fieldbyname('NAMA').AsString);
      listtpsid.Add(fieldbyname('ID_TPS').AsString);
      if(tps=fieldbyname('ID_TPS').asstring)then begin
        cbtps.itemindex:=counter;
        lblTps.caption := fieldbyname('NAMA').AsString;
      end;
      counter:=counter+1;
      next;
    end;
    //cbtps.Visible:=false;
  end;
end;

procedure tedittps.refreshdata;
var kdesa:String;
begin
//
  sDesa:='';
  lblKet.Caption:='';
  cRow:=2;  //wadah row
  dRow:=1;  //menyimpan banyaknya data
  bHal:=20;  //pemilih per halaman
  lblHal.Caption:= '-';
  lblPemilih.Caption:='';
  txtHal.Enabled:=False;
  btnPrev.Enabled:=False;
  btnNext.Enabled:=False;
  btnHapus.Enabled:=false;
  //get nama tps
  //get alamat tps
  with ZTable1 do begin
    active:=false;
    TableName:='daftar_tps';
    filter:='ID_TPS='+tps;
    filtered:=true;
    Active:=true;
    first;
    //Label6.Caption:=Label6.Caption+FieldByName('NAMA').AsString;
    GroupBox3.Caption:='Pemilih yang Belum Terdaftar di TPS '+FieldByName('NAMA').AsString;
    btnTambahOrang.Caption:='Daftarkan yang Dipilih ke TPS '+FieldByName('NAMA').AsString;
    btnTambahSemua.Caption:='Daftarkan Semua ke TPS '+FieldByName('NAMA').AsString;
    txtAlamatTPS.Text:=FieldByName('ALAMAT').AsString;
    kdesa:=FieldByName('KODE_DESA').AsString;
  end;
  //get ket
  with ZTable1 do begin
    active:=false;
    TableName:='kabkot';
    filter:='KODE='+quotedStr(Copy(kdesa,1,2));
    filtered:=true;
    Active:=true;
    first;
    lblKet.Caption:=fieldbyname('TAMPIL').AsString+', ';
  end;
  with ZTable1 do begin
    active:=false;
    TableName:='kecamatan';
    filter:='KODE_KECAMATAN='+quotedStr(Copy(kdesa,1,5));
    filtered:=true;
    Active:=true;
    first;
    lblKet.Caption:=lblKet.Caption+'KECAMATAN '+fieldbyname('TAMPIL').AsString+', ';
  end;
  with ZTable1 do begin
    active:=false;
    TableName:='desa';
    filter:='KODE_DESA='+quotedStr(kdesa);
    filtered:=true;
    Active:=true;
    first;
    lblKet.Caption:=lblKet.Caption+'KELURAHAN/DESA '+fieldbyname('TAMPIL').AsString+' : ';
  end;
  //grid 1
  sgTPS1.Cells[0,0]:='NO';
  sgTPS1.Cells[1,0]:='NIK';
  sgTPS1.Cells[2,0]:='NKK';
  sgTPS1.Cells[3,0]:='Nama';
  sgTPS1.Cells[4,0]:='Tempat Lahir';
  sgTPS1.Cells[5,0]:='Tanggal Lahir';
  sgTPS1.Cells[6,0]:='Nama Ibu';
  sgTPS1.Cells[7,0]:='Agama';
  sgTPS1.Cells[8,0]:='Perkawinan';
  sgTPS1.Cells[9,0]:='Jenis Kelamin';
  sgTPS1.Cells[10,0]:='Kelainan';
  sgTPS1.Cells[11,0]:='Alamat';
  sgTPS1.Cells[12,0]:='RW';
  sgTPS1.Cells[13,0]:='RT';
  sgTPS1.Cells[14,0]:='Dusun';
  sgTPS1.Cells[15,0]:='Ket';
  sgTPS1.Cells[16,0]:='Desa';
  sgTPS1.Cells[17,0]:='ID_USER';
  sgTPS1.RowCount:=cRow;
  //tampil grid : fetch data, string tampil
  sdesa:=kdesa;
  getdata;
  if (ct>0) then tampilhalaman(1);
  
  //-------------------------TAMBAH ORANG
  ZQuery2.Active := False;
  ZQuery2.SQL.Text:='SELECT DISTINCT(RW) FROM pemilih_'+sdesa+' ORDER BY RW ASC';
  ZQuery2.Active := True;
  ZQuery2.First;  //tabel simpan
  rwcombo.Clear;
  while (not ZQuery2.Eof) do
  begin
    rwcombo.Items.Add(ZQuery2.FieldByName('RW').AsString);
    ZQuery2.Next;
  end;
  ZTable1.Active := False;
  ZTable1.TableName := 'kabkot';
  ZTable1.Active := True;
  ZTable1.First;
  sKabkot:='';
  sKec:='';
  crow2:=2;
  drow2:=1;
  Label1.Caption:= '-';
  Edit1.Enabled:=False;
  Button2.Enabled:=False;
  Button1.Enabled:=False;
  //grid 1
  StringGrid1.Cells[0,0]:='NO';
  StringGrid1.Cells[1,0]:='NIK';
  StringGrid1.Cells[2,0]:='NKK';
  StringGrid1.Cells[3,0]:='Nama';
  StringGrid1.Cells[4,0]:='Tempat Lahir';
  StringGrid1.Cells[5,0]:='Tanggal Lahir';
  StringGrid1.Cells[6,0]:='Nama Ibu';
  StringGrid1.Cells[7,0]:='Agama';
  StringGrid1.Cells[8,0]:='Perkawinan';
  StringGrid1.Cells[9,0]:='Jenis Kelamin';
  StringGrid1.Cells[10,0]:='Kelainan';
  StringGrid1.Cells[11,0]:='Alamat';
  StringGrid1.Cells[12,0]:='Kota';
  StringGrid1.Cells[13,0]:='Kecamatan';
  StringGrid1.Cells[14,0]:='Desa';
  StringGrid1.Cells[15,0]:='RW';
  StringGrid1.Cells[16,0]:='RT';
  StringGrid1.Cells[17,0]:='Ket';
  StringGrid1.Cells[18,0]:='ID_USER';
  StringGrid1.RowCount:=cRow;

  SetLength(bTPS,1);
end;

procedure TeditTps.getData; //cari jumlah
var
  i:Integer;
  que, curdesa, curkec:String;
begin
  //untuk satu kecamatan
  ct:=0;
  curkec := copy(sdesa,1,5);
  with ZQuery4 do begin
    active :=false;
    sql.Clear;
    sql.Text:='SELECT * FROM desa WHERE KODE_KECAMATAN = '+quotedStr(curkec);
    active:= true;
    first;
    while not eof do begin
      curdesa:=fieldbyname('KODE_DESA').AsString;
      zquery1.active:=false;
      zquery1.sql.Clear;
      que:='SELECT COUNT(*) FROM pemilih_'+curdesa+' WHERE TPS='+tps;
      zquery1.sql.Text:=que;
      zquery1.Active:=true;
      zquery1.first;
      ct:=ct+ZQuery1.Fields[0].AsInteger;

      next;
    end;
  end;

  jHal:=ct div bHal;
  if ((ct mod bHal)>0) then jHal:=jHal+1;
  lblPemilih.Caption:='Jumlah ='+IntToStr(ct);
end;

procedure TeditTps.cbtpsChange(Sender: TObject);
begin
  tps:=listtpsid[cbtps.itemindex];
  refreshdata;
end;

procedure TeditTps.cxGrid1DBTableView1CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  btnHapus.Enabled:=true;
end;

end.

